<template>
    <div style="height:100%;">
        <Row>
            <Col span="24" class="col-margin"> 字典分类：
            <Select v-model="searchModel.GroupCode" style="width:100px" placeholder="全部" @on-change="getPageData()">
                <Option value="">全部</Option>
                <Option v-for="item in groupList" :value="item.GroupCode" :key="item">{{ item.GroupName }}</Option>
            </Select>
            字典文本：
            <Input v-model="searchModel.DictText" placeholder="请输入查询的字典值..." style="width: 150px"></Input>
            状态
            <Select v-model="searchModel.Status" style="width:100px" placeholder="全部" @on-change="getPageData()">
                <Option value="">全部</Option>
                <Option value="1">启用</Option>
                <Option value="0">停用</Option>
            </Select>
            <Button icon="ios-search" type="ghost" @click="getPageData()">查询</Button>
            </Col>
        </Row>
        <Row>
            <Col span="24" class="col-margin">
            <Button icon="plus" type="primary" @click="showType = true">新增</Button>
            <!--<Button icon="android-done-all" type="primary" @click="multiSelect">批量删除</Button>-->
            </Col>
        </Row>
        <Row>
            <Col span="24" class="col-margin">
            <Table size="small" @on-select="selectSingel" @on-select-all="selectAll" :height="clientHeight" border :context="ctx" :columns="columnsList" :data="datas.rows" no-data-text='暂无数据...'></Table>
            </Col>
        </Row>
        <Row>
            <Col span="24">
            <Page class="page-style" :total="datas.total" placement="top" @on-change="getPageNumber" :page-size="searchModel.PageSize" :page-size-opts='pageSizeOpts' show-sizer @on-page-size-change="getPageSize" show-total show-elevator></Page>
            </Col>
        </Row>
        <Modal v-model="showType" :mask-closable="false" :closable="false">
            <p slot="header" class="title">
                <Icon type="ios-paper" size="20"></Icon>
                <span style="font-size: 20px">数据字典编辑</span>
            </p>
            <p slot="close">
                <Icon type="close-circled" color="red" size=25></Icon>
            </p>
            <div slot="footer">
                <Button type="primary" @click="save('dataModel')">提交</Button>
                <Button type="ghost" @click="cancel('dataModel')" style="margin-left: 8px">取消</Button>
            </div>
            <Form ref="dataModel" :model="dataModel" :rules="ruleValidate" :label-width="80">
                <Form-item style="display:none;" prop="Id">
                    <Input v-model="dataModel.Id"></Input>
                </Form-item>
                <Form-item label="分组名称" prop="GroupName">
                    <Input v-model="dataModel.GroupName" placeholder="请输入分组名称"></Input>
                </Form-item>
                <Form-item label="分组代码" prop="GroupCode">
                    <Input v-model="dataModel.GroupCode" placeholder="请输入分组代码"></Input>
                </Form-item>
                <Form-item label="字典文本" prop="DictText">
                    <Input v-model="dataModel.DictText" placeholder="请输入分组代码"></Input>
                </Form-item>
                <Form-item label="拼音缩写" prop="PinYin">
                    <Input v-model="dataModel.PinYin" placeholder="请输入分组代码"></Input>
                </Form-item>
                <Form-item label="字典值" prop="DictValue">
                    <Input v-model="dataModel.DictValue" placeholder="请输入分组代码"></Input>
                </Form-item>
                <Form-item label="状态" prop="Status">
                    <Select v-model="dataModel.Status" placeholder="请选择状态">
                        <Option value="1">启用</Option>
                        <Option value="0">停用</Option>
                    </Select>
                </Form-item>
                <Form-item label="排序" prop="Sort">
                    <Input v-model="dataModel.Sort" placeholder="请输入分组代码"></Input>
                </Form-item>
                <Form-item label="备注" prop="Remark">
                    <Input v-model="dataModel.Remark" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder="备注长度请小于100个字符..."></Input>
                </Form-item>
            </Form>
        </Modal>
    </div>
</template>
<script>
export default {
    data() {
        return {
            showType: false,//编辑框弹出状态
            ruleValidate: {//验证模型
                GroupName: [{ required: true, message: '分组名称不能为空', trigger: 'blur' }],
                GroupCode: [{ required: true, message: '分组代码不能为空', trigger: 'blur' }],
                DictText: [{ required: true, message: '字典文本不能为空', trigger: 'blur' }],
                PinYin: [{ required: false, message: '', trigger: 'blur' }],
                DictValue: [{ required: true, message: '字典值不能为空', trigger: 'blur' }],
                Sort: [{ required: false, message: '排序不能为空', trigger: 'blur' }],
                Remark: [
                    { required: false, message: '备注长度请小于100个字符', trigger: 'blur' },
                    { type: 'string', max: 100, message: '备注长度请小于100个字符', trigger: 'blur' }
                ]
            },
            dataModel: {//编辑模型
                Id: 0,
                GroupName: "",
                GroupCode: "",
                DictText: "",
                PinYin: "",
                DictValue: "",
                Status: "1",
                Sort: "1",
                Remark: ""
            },
            clientHeight: window.innerHeight - 260,//表格默认高度
            groupList: [],//字典分组列表
            searchModel: { PageNumber: 1, PageSize: 20, DictText: "", GroupCode: "" },//查询模型
            pageSizeOpts: [20, 40, 50],//默认分页最大设置
            datas: {},//table数据对象
            dataIds: [],
            ctx: this,//table数据上下文
            columnsList: [//表头对象
                // { type: 'selection', width: 60, align: 'center' },//多选设置
                { title: "分组名称", key: "GroupName", width: 110 },
                { title: "分组代码", key: "GroupCode", width: 150 },
                { title: "字典文本", key: "DictText" , width: 250},
                { title: "拼音缩写", key: "PinYin", width: 110 },
                { title: "字典值", key: "DictValue", width: 150 },
                {
                    title: "状态", key: "Status", width: 70, render(row, column, index) {
                        if (row.Status === '1') {
                            return "启用";
                        } else {
                            return "停用";
                        }
                    }
                },
                { title: "排序", key: "Sort", width: 70 },
                { title: "备注", key: "Remark"},
                {
                    title: "操作", key: "Id", width: 200, align: 'center',
                    render: (h, params) => {
                        return h('div', [
                            h('Button', {
                                class:{
                                    ieditColor:true
                                },
                                props: {
                                    size: 'small',
                                    type:'ghost',
                                    icon: 'ios-compose'
                                }, style: {
                                    marginRight: '5px'
                                }, on: {
                                    click: () => {
                                        this.editInfo(params.row.Id)
                                    }
                                }
                            }, '编辑'),
                            h('Button', {
                                class:{
                                    ideleteColor:true
                                },
                                props: {
                                    size: 'small',
                                    type:'ghost',
                                    icon: 'ios-trash-outline'
                                }, style: {
                                    marginRight: '5px'
                                }, on: {
                                    click: () => {
                                        this.delInfo(params.row.Id)
                                    }
                                }
                            }, '删除'),
                        ]);
                    }
                }]
        }
    },
    mounted() {
        //table高度计算方法
        const that = this;
        window.onresize = () => {
            return (() => {
                that.clientHeight = window.innerHeight - 260;
            })()
        };
    },
    methods: {
        //获取当前页码
        getPageNumber(pageIndex) {
            this.searchModel.PageNumber = pageIndex;
            this.getPageData();
        },
        //获取当前分页大小
        getPageSize(pageSize) {
            this.searchModel.PageSize = pageSize;
            this.getPageData();
        },
        //获取this.datas: {},//table数据对象
        getPageData() {
            var self = this;
            self.$http.post('Dict/PostPageList', this.searchModel).then(function (res) {
                if (res.data.status === 1) {
                    self.datas = res.data;
                }
                else {
                    self.$Modal.error({ title: '提示信息', content: res.data.message });
                }
            });
        },
        //编辑获取对象传入id
        editInfo(id) {//编辑
            var self = this;
            this.$http.get("Dict", { params: { id: id } }).then(//获取数据编辑项的值
                function (res) {
                    if (res.data.status == 1) {
                        self.dataModel = res.data.model;
                        self.modalShow();//打开modal
                    }
                    else {
                        self.$Modal.error({ title: "提示信息", content: "编辑数据加载失败!!!" });
                    }
                }
            );
        },
        //删除对象
        delInfo(id) {
            var self = this;
            self.$Modal.confirm({
                title: '提示信息',
                content: '<p>数据删除后不可恢复，确定要删除？</p>',
                onOk: () => {
                    this.$http.delete("Dict", { params: { id: id } }).then(
                        function (res) {
                            if (res.data.status == 1) {
                                self.$Notice.success({ title: '删除成功！', desc: false });
                                self.getPageData();
                                self.getGroupList();
                            }
                            else {
                                self.$Modal.error({ title: "提示信息", content: "数据删除失败!!!" });
                            }
                        }
                    );
                }
            });
        },
        //保存方法
        save(modelname) {
            var self = this;
            this.$refs[modelname].validate((valid) => {
                if (valid) {
                    if (this.dataModel.Id === 0) {
                        this.$http.post('Dict', this.dataModel).then(
                            function (res) {
                                if (res.data.status == 1) {
                                    self.getPageData();
                                    self.getGroupList();
                                    self.restModel(modelname);//重置model
                                    self.modalHide();//关闭modal
                                    self.$Notice.success({ title: '保存成功！', desc: false });
                                }
                                else {
                                    self.$Modal.warning({ title: "提示信息", content: "数据字典添加失败,请与管理员联系!" });
                                }
                            });
                    } else {
                        this.$http.put('Dict', this.dataModel).then(
                            function (res) {
                                if (res.data.status == 1) {
                                    self.getPageData();
                                    self.getGroupList();
                                    self.restModel(modelname);//重置model
                                    self.modalHide();//关闭modal
                                    self.$Notice.success({ title: '更新成功！', desc: false });
                                }
                                else {
                                    self.$Modal.warning({ title: "提示信息", content: "数据字典更新失败,请与管理员联系!" });
                                }
                            });
                    }
                }
            });
        },
        //编辑框打开
        modalShow() {
            this.showType = true;
        },
        //编辑框关闭
        modalHide() {
            this.showType = false;
        },
        //取消方法
        cancel(modelname) {
            this.$refs[modelname].resetFields();//重置model
            this.showType = false;//关闭modal
        },
        //重置model
        restModel(modelname) {
            this.$refs[modelname].resetFields();//重置model
        },
        //获取下拉数据字典分组方法
        getGroupList() {
            var self = this;
            this.$http.get("Dict/GetGroupList").then(//获取字典分组列表
                function (res) {
                    if (res.data.status == 1) {
                        self.groupList = res.data.model;
                    }
                    else {
                        self.$Modal.error({ title: "提示信息", content: "数据加载失败!!!" });
                    }
                }
            );
        },
        //全选方法
        selectAll(selection) {
            this.dataIds = selection;
        },
        //checkbox选中方法
        selectSingel(selection, row) {
            this.dataIds = selection;
        },
        //checkbox取消选中方法
        selecChange(selection) {
            this.dataIds = selection;
        },
        //批量删除
        multiSelect() {
            var self = this;
            if (this.dataIds.length !== 0) {
                self.$Modal.confirm({
                    title: '提示信息',
                    content: '<p>数据删除后不可恢复，确定要删除？</p>',
                    onOk: () => {
                        var ids = this.getDataIds(this.dataIds);
                        this.$http.post("Dict/PostBatchDelete", ids).then(
                            function (res) {
                                if (res.data.status == 1) {
                                    self.dataIds = [];
                                    self.$Notice.success({ title: '删除成功！', desc: false });
                                    self.getPageData();
                                    self.getGroupList();
                                }
                                else {
                                    self.$Modal.error({ title: "提示信息", content: "数据删除失败!!!" });
                                }
                            }
                        );
                    }
                });
            } else {
                self.$Modal.warning({ title: "提示信息", content: "请选择需要删除的数据!" });
            }

        },
        getDataIds(idList) {
            var ids = [];
            if (idList.length !== 0) {
                for (var i = 0; i < idList.length; i++) {
                    ids.push(idList[i].Id);
                }
            }
            return ids;
        }
    },
    //创建Vue页面方法
    created: function () {
        //创业Vue前调用获取this.datas: {},//table数据对象
        this.getPageData();
        this.getGroupList();
    },
    watch: {
        //需要监听的在此处新增监听对象
    }
}
</script>
